package com.lixw.saa.config;

import com.alibaba.cloud.ai.dashscope.api.DashScopeApi;
import com.alibaba.cloud.ai.dashscope.chat.DashScopeChatModel;
import com.alibaba.cloud.ai.dashscope.chat.DashScopeChatOptions;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.chat.prompt.ChatOptions;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @ClassName: LLMConfig
 * @Description:
 * @Author: xuweiLi
 * @Create: 2025/10/2 17:03
 **/
@Configuration
public class LLMConfig {

    private final String DEEPSEEK_MODEL = "deepseek-v3";
    private final String QWEN_MODEL = "qwen-plus";

    @Value("${spring.ai.dashscope.api-key}")
    private String apiKey;

    @Bean
    public ChatModel deepseekChatModel() {
        return DashScopeChatModel.builder()
                .dashScopeApi(DashScopeApi.builder()
                        .apiKey(apiKey)
                        .build()
                )
                .defaultOptions(DashScopeChatOptions.builder()
                        .withModel(DEEPSEEK_MODEL) //相当于底层API 指定当前chatModel默认支持Deepseek
                        .build()
                )
                .build();
    }

    @Bean
    public ChatModel qwenChatModel() {
        return DashScopeChatModel.builder()
                .dashScopeApi(DashScopeApi.builder()
                        .apiKey(apiKey)
                        .build()
                )
                .defaultOptions(DashScopeChatOptions.builder()
                        .withModel(QWEN_MODEL)
                        .build()
                )
                .build();
    }

    @Bean
    public ChatClient deepseekChatClient(@Qualifier("deepseekChatModel") ChatModel chatModel) {
        return ChatClient.builder(chatModel)
                /*
                    提供清晰的配置层次结构：

                    底层 (ChatModel): 连接级别配置 (API Key, 基础URL等)

                    中层 (ChatClient): 应用级别默认配置

                    高层 (调用时): 具体请求的个性化配置
                 */
                .defaultOptions(ChatOptions.builder().model(DEEPSEEK_MODEL).build()) // 应用层API覆盖底层ChatModel的默认行为
                .build();
    }

    @Bean
    public ChatClient qwenChatClient(@Qualifier("qwenChatModel") ChatModel chatModel) {
        return ChatClient.builder(chatModel)
                //.defaultOptions(ChatOptions.builder().model(QWEN_MODEL).build())
                .build();
    }
}